package com.enwei.activitys;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.enwei.base.BaseActivity;
import com.enwei.utils.CacheKeys;
import com.enwei.utils.Constans;
import com.enwei.utils.SpfHelper;
import com.enwei.utils.http.HttpApi;
import com.enwei.utils.http.HttpCallBack;
import com.ew.action.R;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RestPwdActivity extends BaseActivity {
	private EditText et_phone;
	private EditText et_code;
	private TimeCount time;
	private Button btn;
	private static String APPKEY = "81932beaa958";
	private static String APPSECRET = "aec12c686401d2486e690a130333866b";
	final int MAX_LENGTH = 11;
	int Rest_Length = MAX_LENGTH;
	public String phString;
	
	private EditText mPassword;
	private ProgressDialog dialog = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rest_pwd,true,true);
		/** 手机号码 */
		et_phone = (EditText) findViewById(R.id.et_phone);
		/** 验证码 */
		et_code = (EditText) findViewById(R.id.et_code);
		mPassword = (EditText) findViewById(R.id.et_password);
		
		/** 发送验证码按钮 */
		btn = (Button) findViewById(R.id.btn);
		time = new TimeCount(60000, 1000);// 构造CountDownTimer对象\
		SMSSDK.initSDK(this, APPKEY, APPSECRET);
		EventHandler eh = new EventHandler() {
			@Override
			public void afterEvent(int event, int result, Object data) {

				Message msg = new Message();
				msg.arg1 = event;
				msg.arg2 = result;
				msg.obj = data;
				handler.sendMessage(msg);
			}
		};
		SMSSDK.registerEventHandler(eh);
		/**
		 * 初始化btn按钮
		 * */
		Intent intent = getIntent();
		String mobilephone = intent.getStringExtra("phone");
		et_phone.setText(mobilephone);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				clickcode(view);
			}
		});
	}



	

	

	public void clickcode(View view) {
		String phone = et_phone.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			Toast.makeText(this, "手机号不能为空", 0).show();
			return;
		} else {
			SMSSDK.getVerificationCode("86",phone);
			time.start();
		}
	}

	public void clickreg(View view) {
		String code = et_code.getText().toString().trim();
		String phone = et_phone.getText().toString().trim();
		String pwd = mPassword.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			Toast.makeText(this, "手机号不能为空", 0).show();
			return;
		}
		if (TextUtils.isEmpty(code)) {
			Toast.makeText(this, "验证码不能为空", 0).show();
			return;
		}
		if(TextUtils.isEmpty(pwd)){
			Toast.makeText(this, "密码不能为空", 0).show();
			return;
		}
		SMSSDK.submitVerificationCode("86", phone, et_code.getText()
				.toString());	
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			int event = msg.arg1;
			int result = msg.arg2;
			Object data = msg.obj;
			if (result == SMSSDK.RESULT_COMPLETE) {
				// 短信注册成功后，返回MainActivity,然后提示新好友
				if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {// 提交验证码成功
					String phone = et_phone.getText().toString().trim();
					String pwd = mPassword.getText().toString().trim();
					postRegsiter(phone, pwd);
				} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
					Toast.makeText(getApplicationContext(), "验证码已经发送",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getApplicationContext(), "验证码错误",
							Toast.LENGTH_SHORT).show();

				}
			} else {
				((Throwable) data).printStackTrace();
			}
		}
	};

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			btn.setText("获取验证码");
			btn.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			btn.setClickable(false);
			btn.setText("重新发送("+millisUntilFinished / 1000+"s)");
		}
	}
	
	private void postRegsiter(String phoneNumber,String pwd){
		showDialog("提交数据，请稍后...");
		String url = Constans.USER_FORGET;
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("phone", phoneNumber));
		nameValuePairs.add(new BasicNameValuePair("password", pwd));
		HttpApi http = new HttpApi(this,HttpMethod.POST);
		http.sendHttpRequest(url, nameValuePairs, new HttpCallBack() {
			
			@Override
			public void onSuccess(String data) {
				Toast.makeText(getApplicationContext(), "密码修改成功!", Toast.LENGTH_SHORT).show();
				finish();
				SpfHelper.setParam(RestPwdActivity.this, CacheKeys.LOGIN_USER, "");
				sendBroadcast(new Intent(PersonalActivity.ACTION_NAME));
				dialog.dismiss();
				
			}
			@Override
			public void onFailure() {
				Toast.makeText(getApplicationContext(), "密码修改成功！", Toast.LENGTH_SHORT).show();
				dialog.dismiss();
				super.onFailure();
			}
		});
	}
	
	private void showDialog(String str){
		dialog = ProgressDialog.show(this, null, str, true, true);
	}
}